package com.tanhua.system.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.system.mapper.LoggingMapper;
import com.tanhua.system.vo.DashBoardVo;
import com.tanhua.system.vo.Tasks;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Mr.blue
 * @Description:
 * @Date: Created in 16:49 2021/1/19
 * @ModifiedBy:
 */
@Service
public class DashboardServicePlus {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private LoggingMapper loggingMapper;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;


    /**
     * 新增、活跃用户、次日留存率
     * @param sd
     * @param ed
     * @param type
     * @return
     */
    public DashBoardVo dashBoardMsg(Long sd, Long ed, Integer type) {
        DashBoardVo dashBoardVo = new DashBoardVo();
        List<Tasks> thisYearList = new ArrayList<>();
        List<Tasks> lastYearList = new ArrayList<>();

        //将区域分成12个部分
        Long avg = (ed - sd) / 12;
        List<Long> thisYearTimeList = new ArrayList<>();
        List<Long> lastYearTimeList = new ArrayList<>();

        Long lastYearSd = DateUtils.addYears(new Date(sd), -1).getTime();
        for (int i = 0; i <= 12; i++) {
            thisYearTimeList.add(sd + avg * i);
            lastYearTimeList.add(lastYearSd + avg * i);
        }

        //设置一段时间内的用户数
        List<Integer> counts = null;

        //设置类型
        switch (type) {
            //新增
            case 101: {
                counts = this.userService.saveUser(thisYearTimeList, lastYearTimeList);
                break;
            }
            //活跃用户
            case 102: {
                counts = this.quanZiApi.queryCommentCounts(thisYearTimeList, lastYearTimeList);
                break;
            }
            //次日留存率
            case 103: {
                List<List<Long>> nextList = this.userService.queryNextDay(thisYearTimeList, lastYearTimeList);
                counts = this.quanZiApi.queryNextDay(nextList, thisYearTimeList, lastYearTimeList);
                break;
            }
            default:{
                break;
            }
        }

        if (CollectionUtils.isEmpty(counts)) {
            return null;
        }

        //设置每个时间段的新增用户
        for (int i = 1; i <= counts.size(); i++) {
            Tasks yearData = new Tasks();
            yearData.setAmount(counts.get(i-1));
            //判断是今年的还是去年的 奇数是今年的 偶数是去年的
            if (i % 2 == 1) {
                yearData.setTitle(String.valueOf((i + 1) / 2));
                thisYearList.add(yearData);
            } else {
                yearData.setTitle(String.valueOf(i / 2));
                lastYearList.add(yearData);
            }

        }
        dashBoardVo.setThisYear(thisYearList);
        dashBoardVo.setLastYear(lastYearList);

        return dashBoardVo;
    }
}
